/*
https://leetcode.cn/problems/fruit-into-baskets/

904.水果成篮
medium, 李佳韵 2024.09.01
滑动窗口

*/

#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
#include<vector>
using namespace std;
class Solution {
public:
    int totalFruit(vector<int>& fruits) {

        int j = 0, i;
        int c1 = 0, c2 = 0, num = 0;
        for (i = 0; i < fruits.size(); ++i) {
            if (fruits[i] != fruits[c1] && fruits[i] != fruits[c2]) {
                if (c1 == c2)
                    c2 = i;
                else {
                    c2 = i;
                    c1 = i - 1;
                    if (i - j > num)
                        num = i - j;
                    j = i;
                    while (fruits[j - 1] == fruits[c2] ||
                        fruits[j - 1] == fruits[c1]) {
                        --j;
                    }
                    c1 = j;
                }
            }
        }
        return i - j > num ? i - j : num;
    }
};

int main()
{
    Solution solution;
    vector<int>fruits = { 1,2,1 };
    int result = solution.totalFruit(fruits);
    cout << "可以采摘最多的水果树的数量为：" << result << endl;
    return 0;
}